package com.hepinghospital.user.ui.activity.userinfo;

import android.content.Intent;
import android.util.Log;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.base.BaseApplication;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;

import butterknife.BindView;
import butterknife.OnClick;
import jz.joyoung.robot.util.UtilString;

/**
 * @author DELL
 * @time 2020/11/16 10:21
 * Created by lbj on 2020/11/16 10:21.
 */
public class UserInfoEditActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;
    @BindView(R.id.edt_userinfo)
    EditText edtUserinfo;

    private String editType;

    @Override
    protected void initViews() {
    }

    @Override
    protected void initData() {
        editType = getIntent().getStringExtra("editType");
        String editData = getIntent().getStringExtra("editData");

        Log.e("编辑类型----", editType);
        if (!UtilString.isEmpty(editData)) {
            edtUserinfo.setText(editData);
        }
        if (!UtilString.isEmpty(editType)) {
            if ("name".equals(editType)) {
                tvTitle.setText("姓名");
                if (UtilString.isEmpty(editData)) {
                    edtUserinfo.setHint("请输入姓名");
                }
            } else if ("addFeedback".equals(editType)) {
                //意见反馈
                tvTitle.setText("意见反馈");
                edtUserinfo.setHint("如果您有好的意见或使用建议，请告诉我们\n" +
                        "我们将认真评估");
            } else if ("telphone".equals(editType)) {
                tvTitle.setText("电话");
                if (UtilString.isEmpty(editData)) {
                    edtUserinfo.setHint("请输入电话");
                }
            }
        }
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_edit_userinfo;
    }

    @OnClick(R.id.tv_submit)
    public void onViewClicked() {

        if (UtilString.isEmpty(edtUserinfo.getText().toString().trim())) {
            ToastUtil.showToast(mContext, "请输入相关内容");
        } else {

            if (!UtilString.isEmpty(editType)) {
                if ("name".equals(editType)) {
                    //修改昵称接口
                    RestProxy.getInstance().changeNickName(edtUserinfo.getText().toString().trim(), new BaseObserver<String>() {
                        @Override
                        public void onSuccess(String s) {
                            try {
                                Intent intent = new Intent();
                                BaseApplication.name = edtUserinfo.getText().toString().trim();
                                intent.putExtra("editData", edtUserinfo.getText().toString().trim());
                                setResult(RESULT_OK, intent);
                                finish();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onError(Throwable e) {
                            ToastUtil.onError(UserInfoEditActivity.this, e);
                        }
                    });
                } else if ("addFeedback".equals(editType)) {
                    //意见反馈
                    RestProxy.getInstance().addFeedback(edtUserinfo.getText().toString().trim(), new BaseObserver<String>() {

                        @Override
                        public void onError(Throwable e) {
                            ToastUtil.onError(UserInfoEditActivity.this, e);
                        }

                        @Override
                        public void onSuccess(String result) {
                            try {
                                ToastUtil.showToast(mContext, "提交成功,感谢您的宝贵意见");
                                finish();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } else if ("telphone".equals(editType)) {

                }
            }
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}
